package cn.jinjimi.app.main.adapter.recycler;

import android.animation.Animator;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

/**
 * Created by Roy
 * 处理动画逻辑 以及 点击事件
 * Date: 15/9/21
 * @author zhenhua
 */
public abstract class MyBaseRecyclerAdapter<E,VH extends MyBaseRecyclerAdapter.MyViewHolder> extends MyBaseRecyclerDataAdapter<E,VH> {

    /** 长按短按响应事件 **/
    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    public OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        holder.position = position;
        if (!isFirstOnly || position > mLastPosition) {
            for (Animator anim : getAnimators(holder.itemView)) {
                anim.setDuration(mDuration).start();
                anim.setInterpolator(mInterpolator);
            }
            mLastPosition = position;
        } else {
            clear(holder.itemView);
        }
    }

    public abstract class MyViewHolder extends RecyclerView.ViewHolder {
        int position;

        public MyViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(v, position);
                    }
                }
            });

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemLongClick(v, position);
                    }
                    return false;
                }
            });
        }

        public int getHolderPosition() {
            return position;
        }
    }


    /** 动画效果 **/
    private int mDuration = 300;
    private Interpolator mInterpolator = new LinearInterpolator();
    private int mLastPosition = -1;
    private boolean isFirstOnly = true;

    public void setAnimateDuration(int duration) {
        mDuration = duration;
    }

    public void setAnimateInterpolator(Interpolator interpolator) {
        mInterpolator = interpolator;
    }

    public void setAnimateStartPosition(int start) {
        mLastPosition = start;
    }

    public void setAnimateFirstOnly(boolean firstOnly) {
        isFirstOnly = firstOnly;
    }

    protected abstract Animator[] getAnimators(View view);

    public static void clear(View v) {
        ViewCompat.setAlpha(v, 1);
        ViewCompat.setScaleY(v, 1);
        ViewCompat.setScaleX(v, 1);
        ViewCompat.setTranslationY(v, 0);
        ViewCompat.setTranslationX(v, 0);
        ViewCompat.setRotation(v, 0);
        ViewCompat.setRotationY(v, 0);
        ViewCompat.setRotationX(v, 0);
        ViewCompat.setPivotY(v, v.getMeasuredHeight() / 2);
        ViewCompat.setPivotX(v, v.getMeasuredWidth() / 2);
        ViewCompat.animate(v).setInterpolator(null);
    }
}
